// pages/my_business/my_business.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height:'',
    btnText:'0',
    list:[],
    onsold:[],
    warehouse:[],
  },
  // 点击商品按钮
  btnClick:function(e){
    let goods_id = e.currentTarget.dataset.id
    let index =  e.currentTarget.dataset.index
    if(this.data.btnText == '0'){
        //下架假删除
    this.data.list.splice(index,1)
    this.setData({
      list:this.data.list
    })
    //真删除(下架)
    wx.request({
      url: 'http://121.37.209.61:8888/goods/deal',
      method:'get',
      data:{
        goods_id:goods_id,
        goods_deal:'0'
      },
      success(res){
        if(res.data.code == 200){
          wx.showToast({
            title: '成功下架',
            icon: 'success',
            duration: 1000
          })
        }else{
          wx.showToast({
            title: '下架失败',
            icon: 'error',
            duration: 1000
          })
        }
      }
    })
    }else{
      //上架假删除
      this.data.list.splice(index,1)
      this.setData({
        list:this.data.list
      })
      //真删除(上架)
      wx.request({
        url: 'http://121.37.209.61:8888/goods/deal',
        method:'get',
        data:{
          goods_id:goods_id,
          goods_deal:'1'
        },
        success(res){
          if(res.data.code == 200){
            wx.showToast({
              title: '成功上架',
              icon: 'success',
              duration: 1000
            })
          }else{
            wx.showToast({
              title: '上架失败',
              icon: 'error',
              duration: 1000
            })
          }
        }
      })
    }
  
    
  },
  // 点击已上架
  onsold:function(){
    let user_id = wx.getStorageSync('user').user_id
    //请求数据
    let that = this
    wx.request({
      url: 'http://121.37.209.61:8888/goods/id',
      method:'get',
      data:{
        user_id:user_id
      },
      success(res){
        // (res.data.data);
        let goods = res.data.data
        let onsold = []
        let warehouse = []
        goods.forEach(item => {
            if(item.goods_deal == "1"){
              onsold.push(item)
            }else{
              warehouse.push(item)
            }
        });
        that.setData({
          list:onsold,
          btnText:'0'
        })
      }
    })
    
  },
  //点击仓库
  warehouse:function(){
    let user_id = wx.getStorageSync('user').user_id
    //请求数据
    let that = this
    wx.request({
      url: 'http://121.37.209.61:8888/goods/id',
      method:'get',
      data:{
        user_id:user_id
      },
      success(res){
        let goods = res.data.data
        let onsold = []
        let warehouse = []
        goods.forEach(item => {
            if(item.goods_deal == "1"){
              onsold.push(item)
            }else{
              warehouse.push(item)
            }
        });
        that.setData({
          list:warehouse,
          btnText:'1'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    let user_id = wx.getStorageSync('user').user_id
    // 监听窗口高度
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      },
    })
    //请求数据
    let that = this
    wx.request({
      url: 'http://121.37.209.61:8888/goods/id',
      method:'get',
      data:{
        user_id:user_id
      },
      success(res){
        let goods = res.data.data
        goods.forEach(item => {
            if(item.goods_deal == "1"){
              that.data.onsold.push(item)
            }else{
              that.data.warehouse.push(item)
            }
        });
        that.setData({
          list:that.data.onsold
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})